﻿using ReportSystem_XY.BLL;
using ReportSystem_XY.Common;
using ReportSystem_XY.Models;
using ReportSystem_XY.WinCCArchive;
using System;
using System.Activities.Expressions;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using static ReportSystem_XY.WinCCArchive.Common;

namespace ReportSystem_XY.DAL
{
    public class ReportDataDAL:BQuery<ReportDataModel>
    {
        public DataTable GetStepInfo(ReportNameModel model)
        {
            DataTable dt = new DataTable();
            string strWhere = "";
            SqlParameter[] paras =
            {
                new SqlParameter("@reportname",model.reportname)
            };
            if (model.reporttype==0)
            {          
            //步骤由两部分组成，一部分灭菌带小步骤信息+一部分大步骤信息
                strWhere = "(SELECT * FROM (SELECT PROCESSTYPE,stepno, logtime as StartTime,lead(logtime,1)over (order by logtime) as StopTime from tblStepInfo where reportname=@reportname) T WHERE T.processtype=2) UNION  (select b.PROCESSTYPE,stepno =-1,min(b.starttime) StartTime,max(b.stoptime) as StopTime from (SELECT PROCESSTYPE,logtime as StartTime,lead(logtime,1)over (order by logtime) as StopTime from tblStepInfo where reportname=@reportname) b  where  b.processtype<>2 and b.PROCESSTYPE<9 group by b.processtype) order by processtype";
            
            }

            if (model.reporttype == 1)
            {
                //步骤由两部分组成，一部分灭菌带小步骤信息+一部分大步骤信息
                strWhere =  "SELECT * FROM (SELECT PROCESSTYPE,stepno, logtime as StartTime,lead(logtime,1)over (order by logtime) as StopTime from tblStepInfo where reportname=@reportname) t where t.processtype<9";
            }

            if (model.reporttype == 2)
            {
                strWhere = "select * from v_productInfo where reportname=@reportname";
            }

            return SqlHelper.GetDataTable(strWhere,1,paras);
        }

        public StepTimeInfoModel GetStepTimeInfo(DataTable dt)
        {
            StepTimeInfoModel model = new StepTimeInfoModel();
            //灭菌流程开始和结束的时间
           model.startTime = dt.Compute("MIN(starttime)", "true").ToString();
            model.stopTime = dt.Compute("MAX(stoptime)", "true").ToString();

            //获取灭菌步的开始和结束时间，判断时间间隔是否达标
            model.beginTime = dt.Compute("min(starttime)", "processtype=2 and stepno=7").ToString();
            model.endTime= dt.Compute("max(stoptime)", "processtype=2 and stepno=7").ToString();
            return null;
        }

        public DataTable GetArchiveData(string strIndex, string strColNames, string tfrom, string tto, string timeSpace = "3600", DtySimple simple = DtySimple.First)
        {
            return ArchiveHelper.MergeDataTable(strIndex, strColNames, tfrom, tto, timeSpace, simple);
        }

        public DataTable GetCTArchiveData(string strIndex, string strColNames, string tfrom, string tto, out bool result, string timeSpace = "3600", DtySimple simple = DtySimple.First)
        {
            return ArchiveHelper.MergeCTDataTable(strIndex, strColNames, tfrom, tto,  out result, timeSpace, simple);
        }



        public DataTable GetStepAccuracy(DataTable dt, ReportNameModel model)
        {
            DataTable dtstep = new DataTable();
            string sterStart = dt.Compute("min(starttime)", "processtype=2 and stepno=7").ToString();
            string sterStop = dt.Compute("max(stoptime)", "processtype=2 and stepno=7").ToString();
            double sp =(double) model.time;
            double ts = (double)Convert.ToDateTime(sterStop).Subtract(Convert.ToDateTime(sterStart)).TotalSeconds;
            if (ts != sp)
            {
                //只需要更新灭菌的开始时间
                string rightStart = (Convert.ToDateTime(sterStop).AddSeconds(-1 * sp)).ToString("yyyy-MM-dd HH:mm:ss");
                string strWhere = "update tblStepInfo set logtime=@rightstart WHERE reportname=@reportname and processtype=2 and stepno =7";
                SqlParameter[] paras =
                {
                   new SqlParameter("@reportname",model.reportname),
                   new SqlParameter("@rightstart",rightStart)
                };
                SqlHelper.ExecuteNonQuery(strWhere,1,paras);
                Thread.Sleep(500);
                //调整后，需要更新步骤信息
                dtstep = GetStepInfo(model);             
            }
            else
                dtstep=dt.Copy();
            return dtstep;
        }
    }
}
